1625D - Binary Spiders - CodeForces Solution


bitmasks data structures implementation math sortings trees *2300

Please click on ads to support us..

Python Code:

def main():

    n, k = readIntArr()
    a = readIntArr()

    if k == 0:
        print(n)
        assert n >= 2
        oneLineArrayPrint(list(range(1, n + 1)))
        return
    
    
    trie = [-1] * (300000 * 30 * 3 + 100)
    I = 3
    for idx, x in enumerate(a):
        u = 0
        for i in range(29, -1, -1):
            if (x & (1 << i)) > 0:
                if trie[u + 1] == -1:
                    trie[u + 1] = I
                    I += 3
                u = trie[u + 1]
            else:
                if trie[u] == -1:
                    trie[u] = I
                    I += 3
                u = trie[u]
                trie[u + 2] = idx  
    msb = -1
    for i in range(29, -1, -1):
        if (k & (1 << i)) > 0:
            msb = i
            break
    assert msb != -1

    ans = []
    st = [(0, 29)]
    while st:
        u, bit = st.pop()
        if bit > msb:
            if trie[u] != -1:
                st.append((trie[u], bit - 1))
            if trie[u + 1] != -1:
                st.append((trie[u + 1], bit - 1))
        else:
                        subtree_elements = []
            st2 = [(u, bit)]
            while st2:
                u2, bit2 = st2.pop()
                if bit2 == -1:
                    subtree_elements.append(trie[u2 + 2])
                else:
                    if trie[u2] != -1:
                        st2.append((trie[u2], bit2 - 1))
                    if trie[u2 + 1] != -1:
                        st2.append((trie[u2 + 1], bit2 - 1))

            best_xor = -1
            best_i1 = -1
            best_i2 = -1
            for i1 in subtree_elements:
                u2 = u
                for bit2 in range(bit, -1, -1):
                    if (a[i1] & (1 << bit2)) > 0:
                        if trie[u2] != -1:
                            u2 = trie[u2]
                        else:
                            u2 = trie[u2 + 1]
                    else:
                        if trie[u2 + 1] != -1:
                            u2 = trie[u2 + 1]
                        else:
                            u2 = trie[u2]
                i2 = trie[u2 + 2]
                xor = a[i1] ^ a[i2]
                if xor > best_xor:
                    best_xor = xor
                    best_i1 = i1
                    best_i2 = i2
                assert best_xor != -1
            if best_xor >= k:
                ans.append(best_i1)
                ans.append(best_i2)
            else:
                ans.append(best_i1)


    if len(ans) <= 1:
        print(-1)
    else:
        print(len(ans))
        for i in range(len(ans)):
            ans[i] += 1          oneLineArrayPrint(ans)

    return

import sys
input=sys.stdin.buffer.readline  
def oneLineArrayPrint(arr):
    print(' '.join([str(x) for x in arr]))
def multiLineArrayPrint(arr):
    print('\n'.join([str(x) for x in arr]))
def multiLineArrayOfArraysPrint(arr):
    print('\n'.join([' '.join([str(x) for x in y]) for y in arr]))
 
def readIntArr():
    return [int(x) for x in input().split()]
 
def makeArr(defaultValFactory,dimensionArr):     dv=defaultValFactory;da=dimensionArr
    if len(da)==1:return [dv() for _ in range(da[0])]
    else:return [makeArr(dv,da[1:]) for _ in range(da[0])]
 
def queryInteractive(a, b, c):
    print('? {} {} {}'.format(a, b, c))
    sys.stdout.flush()
    return int(input())
 
def answerInteractive(x1, x2):
    print('! {} {}'.format(x1, x2))
    sys.stdout.flush()
 
inf=float('inf')
 
from math import gcd,floor,ceil
import math
 
for _abc in range(1):
    main()


Comments

Submit
0 Comments
More Questions

954A - Diagonal Walking
39F - Pacifist frogs
1451C - String Equality
386A - Second-Price Auction
1690E - Price Maximization
282B - Painting Eggs
440A - Forgotten Episode
233B - Non-square Equation
628B - New Skateboard
262B - Roma and Changing Signs
755C - PolandBall and Forest
456B - Fedya and Maths
376B - IOU
1623B - Game on Ranges
1118A - Water Buying
1462C - Unique Number
301A - Yaroslav and Sequence
38A - Army
38C - Blinds
1197A - DIY Wooden Ladder
1717D - Madoka and The Corruption Scheme
1296D - Fight with Monsters
729D - Sea Battle
788A - Functions again
1245B - Restricted RPS
1490D - Permutation Transformation
1087B - Div Times Mod
1213B - Bad Prices
1726B - Mainak and Interesting Sequence
1726D - Edge Split